(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('apf.stopPayment.createContinueController', createContinueController);
	createContinueController.$inject = [ '$q', '$filter', '$scope', 'Constants', 'formatService', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'stopPaymentService', 'dateService', "arithService", 'commonService'];
	function createContinueController($q, $filter, $scope, Constants, formatService, modalService, gridService, layoutConfig, validatorConfig, authService, stopPaymentService, dateService, arithService, commonService) {
		var ctrl = this, params = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.stopPayment = {};
		ctrl.init = function() {
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.member.tableConfigs.table, rowSelectionChanged);
			ctrl.innerGridOptions.data = 'vm.stopPayment.memberList';
			ctrl.innerGridOptions.enableRowHeaderSelection = false;
			authService.createContinue(params).then(function(response) {
				if (response.data && response.data.model && response.data.task) {
					ctrl.stopPayment= response.data.model;
					ctrl.stopPayment.taskId = response.data.task.taskId;
				}
			});
		};
		
		ctrl.addStopPayment = function(row) {
			return $q(function(resolved, rejected){
				// params
				var params = {};
				params.memberIds = [];
				params.operation = "S";
				angular.forEach(ctrl.stopPayment.memberList, function(stopPayment, index) {
					params.memberIds.push(stopPayment.memberId);
				});
				modalService.openCustomDialogs({
				   'title': 'title.stoppayment.lookup',
				   'dialogsModule' : 'apf.stopPayment',
				   'params':  params,
				   'btnCallback' : {
						'ok' : function(selectedRows) {			
												
							if (!ctrl.stopPayment.memberList) {								
								ctrl.stopPayment.memberList = [];
							}
							angular.forEach(selectedRows, function(selectedRow, index) {
								ctrl.stopPayment.memberList.push(selectedRow);
							});
							
						},
						'close' : function(){
							rejected();
						}
						
					}		
				});	
			});
					
		};
		
	
		ctrl.createContinueSave = function() {
			if (!ctrl.stopPayment.memberList || ctrl.stopPayment.memberList.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			return authService.createContinueSave(ctrl.stopPayment).then(saveSuccessful, saveFailed);
		};

		ctrl.createContinueSubmit = function() {
			if (!ctrl.stopPayment.memberList || ctrl.stopPayment.memberList.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			return authService.createContinueSubmit(ctrl.stopPayment).then(submitSuccessful, submitFailed);
		};
		
		ctrl.createContinueCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.stopPayment.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createContinueCancel(ctrl.stopPayment).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('apf.workflow.wfTxn.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'apf.workflow.wfTxn.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.stoppayment.create',
					'state' : 'apf.stopPayment.create',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				angular.extend(ctrl.stopPayment, response.data.model);
			}else if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('apf.workflow.wfTxn.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'apf.workflow.wfTxn.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.stoppayment.create',
					'state' : 'apf.stopPayment.create',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}	
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}
	}
})(angular);
